package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysCycle;
import com.ruoyi.common.core.domain.entity.SysTaskCycle;
import com.ruoyi.common.pojo.SwitchTaskCycle;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysCycleMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysTaskCycleMapper;
import com.ruoyi.system.service.SysTaskCycleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangzhe
 * @Date: 2023/9/6
 */
@Service
public class SysTaskCycleServiceImpl extends ServiceImpl<SysTaskCycleMapper, SysTaskCycle> implements SysTaskCycleService {

    @Resource
    SysCycleMapper sysCycleMapper;

    @Resource
    SysPostMapper sysPostMapper;

    @Resource
    SysTaskCycleMapper sysTaskCycleMapper;

    @Override
    public AjaxResult getCycleList() {
        List<SysCycle> sysCycles = sysCycleMapper.selectList(Wrappers.<SysCycle>lambdaQuery());
        return AjaxResult.success(sysCycles);
    }

    @Override
    public AjaxResult getList() {
        List<SysTaskCycle> list = list(Wrappers.<SysTaskCycle>lambdaQuery());

        for (SysTaskCycle sysTaskCycle : list) {
            SysCycle sysCycle = sysCycleMapper.selectById(sysTaskCycle.getCycleId());
            if (ObjectUtil.isNotNull(sysCycle)) {
                sysTaskCycle.setCycleName(sysCycle.getCycle());
            }
            SysPost sysPost = sysPostMapper.selectPostById(sysTaskCycle.getPostId());
            if (ObjectUtil.isNotNull(sysPost)) {
                sysTaskCycle.setPostName(sysPost.getPostName());
            }
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getInfo(Long id) {

        SysTaskCycle sysTaskCycle = getById(id);
        if (ObjectUtil.isNotNull(sysTaskCycle)) {
            SysCycle sysCycle = sysCycleMapper.selectById(sysTaskCycle.getCycleId());
            if (ObjectUtil.isNotNull(sysCycle)) {
                sysTaskCycle.setCycleName(sysCycle.getCycle());
            }
            SysPost sysPost = sysPostMapper.selectPostById(sysTaskCycle.getPostId());
            if (ObjectUtil.isNotNull(sysPost)) {
                sysTaskCycle.setPostName(sysPost.getPostName());
            }
        }

        return AjaxResult.success(sysTaskCycle);
    }

    @Override
    public AjaxResult switchTaskCycle(SwitchTaskCycle param) {
        int result = sysTaskCycleMapper.switchTaskCycle(param);
        return AjaxResult.success(SqlHelper.retBool(result));
    }

    @Override
    public AjaxResult exist(Long postId) {
        List<SysTaskCycle> list = list(Wrappers.<SysTaskCycle>lambdaQuery()
                .eq(SysTaskCycle::getPostId, postId)
        );
        return AjaxResult.success(CollUtil.isNotEmpty(list));
    }
}
